package org.obeonetwork.dsl.spem.extension.ifest.ontology;

import java.io.File;
import java.io.IOException;
import java.util.Set;

import org.obeonetwork.dsl.spem.extension.ifest.ifest.IFEST;
import org.obeonetwork.dsl.spem.extension.ifest.ifest.impl.IfestFactoryImpl;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
import org.semanticweb.owlapi.model.AddImport;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLImportsDeclaration;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.model.PrefixManager;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
public class OntologyConverter {
	public void IFEST2Ontology(IFEST input, String context)
	{
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		File file = new File ("D://Working/ontologies/instance.owl");
		OWLDataFactory factory = manager.getOWLDataFactory();
		
		try {
			IRI instanceIRI = IRI.create(context);
			IRI ifestIRI = IRI.create("http://www.obeonetwork.org/ontologies/spem/ifest.owl");
			PrefixManager pmIfest = new DefaultPrefixManager(ifestIRI.toString()+"#");
			PrefixManager pmInstance = new DefaultPrefixManager(instanceIRI.toString()+"#");
			//creer un nouvel ontologies pour les instances.
			
			OWLOntology ontology = manager.createOntology(instanceIRI);
			OWLImportsDeclaration importDecla = factory.getOWLImportsDeclaration(ifestIRI);
			manager.applyChange(new AddImport(ontology, importDecla));
			OWLClass clsEngineeringDomain = factory.getOWLClass(":EngineeringDomain",pmIfest);
			OWLClass clsViewpoint = factory.getOWLClass(":Viewpoint",pmIfest);
			/*for (EngineeringDomain e: input.getEngineeringDomain())
			{
					OWLNamedIndividual indiv = factory.getOWLNamedIndividual(":"+e.getName().replaceAll(" ", "_"),pmInstance);
					OWLClassAssertionAxiom enginDomainAssertion = factory.getOWLClassAssertionAxiom(clsEngineeringDomain, indiv);
					manager.addAxiom(ontology, enginDomainAssertion);
					for (Viewpoint v:e.getViewpoints())
					{
						OWLNamedIndividual indivViewpoint = factory.getOWLNamedIndividual(":"+v.getName().replaceAll(" ","_"),pmInstance);
						OWLClassAssertionAxiom assertionViewpoint = factory.getOWLClassAssertionAxiom(clsViewpoint, indivViewpoint);
						manager.addAxiom(ontology, assertionViewpoint);
						OWLObjectProperty containsViewpoint = factory.getOWLObjectProperty(":haveViewpoint", pmIfest);
						OWLObjectPropertyAssertionAxiom assertion = factory.getOWLObjectPropertyAssertionAxiom(containsViewpoint, indiv, indivViewpoint);
						manager.addAxiom(ontology, assertion);
					}
			}*/
			try {
				RDFXMLOntologyFormat format = new RDFXMLOntologyFormat();
				format.setPrefix("ins", pmInstance.getDefaultPrefix());
				manager.saveOntology(ontology,format,IRI.create(file.toURI()));
				OWLIMTool.addOWL(file.getAbsolutePath(), instanceIRI.toString());
			} catch (OWLOntologyStorageException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
						
		} catch (OWLOntologyCreationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
public IFEST Ontology2IFEST(String fileName) throws IOException
{
	IfestFactoryImpl ifestfactory = new IfestFactoryImpl();
	IFEST ifest = ifestfactory.createIFEST();
	OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
	
	File file = new File (fileName);
	OWLDataFactory factory = manager.getOWLDataFactory();
	try {
		IRI ifestIRI = IRI.create("http://www.obeonetwork.org/ontologies/spem/ifest.owl");
		PrefixManager pmIfest = new DefaultPrefixManager(ifestIRI.toString()+"#");
		//import metamodel ontology (i don't know why would have error if we don't import)
		manager.loadOntologyFromOntologyDocument(new File("D://Working/Ontologies/ifest.owl"));
		//load model ontology
		OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file);
		
		OWLClass clsEngineeringDomain = factory.getOWLClass(":EngineeringDomain",pmIfest);
		OWLClass clsViewpoint = factory.getOWLClass(":Viewpoint",pmIfest);
		//get Engineering Individuals
		Set<OWLClassAssertionAxiom> individuals = ontology.getClassAssertionAxioms(clsEngineeringDomain);
		/*
		for (OWLClassAssertionAxiom i:individuals)
		{
			OWLNamedIndividual instance= (OWLNamedIndividual) i.getIndividual();
			EngineeringDomain e = ifestfactory.createEngineeringDomain();
			e.setName(instance.getIRI().toString());
			ifest.getEngineeringDomain().add(e);
			OWLObjectProperty containsViewpoint = factory.getOWLObjectProperty(":haveViewpoint", pmIfest);
			//get all properties of the current Engineering Domain
			Set<OWLObjectPropertyAssertionAxiom> objectproperties = ontology.getObjectPropertyAssertionAxioms(instance);
			for (OWLObjectPropertyAssertionAxiom o:objectproperties)
			{
				if (o.getProperty()==containsViewpoint)
				{
					//get all contained viewpoints
					Set<OWLClassAssertionAxiom> classAssViewpoints = ontology.getClassAssertionAxioms(o.getObject());
					for (OWLClassAssertionAxiom v:classAssViewpoints)
					{
						OWLNamedIndividual owlViewpoint = (OWLNamedIndividual) v.getIndividual();
						Viewpoint viewpoint = ifestfactory.createViewpoint();
						viewpoint.setName(owlViewpoint.toStringID());
						e.getViewpoints().add(viewpoint);
					}
					
				}
			}
			
		}*/
		
		return ifest;
		
					
	} catch (OWLOntologyCreationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}
}
}
